<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use Prettus\Validator\Contracts\ValidatorInterface;
use Prettus\Validator\Exceptions\ValidatorException;
use App\Http\Requests\DormitoryRatingCreateRequest;
use App\Http\Requests\DormitoryRatingUpdateRequest;
use App\Repositories\DormitoryRatingRepository;
use App\Validators\DormitoryRatingValidator;
use App\Models\Period;
use App\Models\Building;
use App\Models\RatingCategory;
use App\Presenters\DormitoryRatingPresenter;
use Artisan;

/**
 * Class DormitoryRatingsController.
 *
 * @package namespace App\Http\Controllers;
 */
class DormitoryRatingsController extends Controller
{
    /**
     * @var DormitoryRatingRepository
     */
    protected $repository;

    /**
     * @var DormitoryRatingValidator
     */
    protected $validator;

    /**
     * DormitoryRatingsController constructor.
     *
     * @param DormitoryRatingRepository $repository
     * @param DormitoryRatingValidator $validator
     */
    public function __construct(DormitoryRatingRepository $repository, DormitoryRatingValidator $validator)
    {
        $this->repository = $repository;
        $this->validator  = $validator;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
        $this->repository->setPresenter(DormitoryRatingPresenter::class);
        if (\request()->has('export')) {
            $dormitoryRatings = $this->repository->all();
        } else {
            $dormitoryRatings = $this->repository->with('company')->paginate(\request()->get('limit', 20));
        }

        if (request()->wantsJson()) {

            return response()->json([
                'data' => $dormitoryRatings,
            ]);
        }
        $periods = Period::orderBy('name', 'desc')->limit(24)->pluck('name', 'id');
        $buildings = Building::orderBy('name', 'asc')->pluck('name', 'id');
        $categories = RatingCategory::orderBy('category_name', 'asc')->pluck('category_name', 'id');
        return view('dormitoryRatings.index', compact('dormitoryRatings', 'periods', 'buildings', 'categories'));
    }

    public function create()
    {
        $periods = Period::orderBy('name', 'desc')->limit(24)->pluck('name', 'id');
        $buildings = Building::orderBy('name', 'asc')->pluck('name', 'id');
        $categories = RatingCategory::orderBy('category_name', 'asc')->pluck('category_name', 'id');
        return view('dormitoryRatings.create', compact('periods', 'buildings', 'categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  DormitoryRatingCreateRequest $request
     *
     * @return \Illuminate\Http\Response
     *
     * @throws \Prettus\Validator\Exceptions\ValidatorException
     */
    public function store(DormitoryRatingCreateRequest $request)
    {
        try {

            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);

            $dormitoryRating = $this->repository->create($request->all());

            $response = [
                'message' => 'DormitoryRating created.',
                'data'    => $dormitoryRating->toArray(),
            ];

            if ($request->wantsJson()) {

                return response()->json($response);
            }

            return redirect()->back()->with('message', $response['message']);
        } catch (ValidatorException $e) {
            if ($request->wantsJson()) {
                return response()->json([
                    'error'   => true,
                    'message' => $e->getMessageBag()
                ]);
            }

            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $dormitoryRating = $this->repository->find($id);

        if (request()->wantsJson()) {

            return response()->json([
                'data' => $dormitoryRating,
            ]);
        }

        return view('dormitoryRatings.show', compact('dormitoryRating'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $dormitoryRating = $this->repository->find($id);
        $periods = Period::orderBy('name', 'desc')->limit(24)->pluck('name', 'id');
        $buildings = Building::orderBy('name', 'asc')->pluck('name', 'id');
        $categories = RatingCategory::orderBy('category_name', 'asc')->pluck('category_name', 'id');
        return view('dormitoryRatings.edit', compact('dormitoryRating', 'periods', 'buildings', 'categories'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  DormitoryRatingUpdateRequest $request
     * @param  string            $id
     *
     * @return Response
     *
     * @throws \Prettus\Validator\Exceptions\ValidatorException
     */
    public function update(DormitoryRatingUpdateRequest $request, $id)
    {
        try {

            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);

            $dormitoryRating = $this->repository->update($request->all(), $id);
            Artisan::call('gen:dorm-rating-major', ['--dorm_id' => $dormitoryRating->dorm_id, '--period' => $dormitoryRating->period_id]);

            $response = [
                'message' => 'DormitoryRating updated.',
                'data'    => $dormitoryRating->toArray(),
            ];

            if ($request->wantsJson()) {

                return response()->json($response);
            }

            return redirect()->back()->with('message', $response['message']);
        } catch (ValidatorException $e) {

            if ($request->wantsJson()) {

                return response()->json([
                    'error'   => true,
                    'message' => $e->getMessageBag()
                ]);
            }

            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
        }
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $deleted = $this->repository->delete($id);

        if (request()->wantsJson()) {

            return response()->json([
                'message' => 'DormitoryRating deleted.',
                'deleted' => $deleted,
            ]);
        }

        return redirect()->back()->with('message', 'DormitoryRating deleted.');
    }
}
